'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {

  async showByName() {
    const { ctx } = this;
    ctx.body = await ctx.service.user.findByName(ctx.params.name);
  }

  async showById() {
    const { ctx } = this;
    ctx.body = await ctx.service.user.findById(ctx.params.id);
  }

  async index() {
    const { ctx } = this;
    ctx.body = await ctx.service.user.findAll();
  }

  async create() {
    const { ctx } = this;
    ctx.logger.info(ctx.request.body);
    ctx.body = await ctx.service.user.add(ctx.request.body);
  }

  async update() {
    const { ctx } = this;
    ctx.logger.info(ctx.request.body);
    await ctx.service.user.update(ctx.request.body);
    ctx.body = { code: 200 };
  }

  async deleteById() {
    this.ctx.logger.info(this.ctx.params.id);
    const result = await this.ctx.service.user.deleteById(this.ctx.params.id);
    this.ctx.body = { code: 200, data: result };
  }

  // 接受 json 数据
  async form() {
    const { ctx } = this;
    ctx.body = `form data: ${JSON.stringify(ctx.request.body)}`;
  }

}

module.exports = UserController;
